// pages/news/news.js
const app = getApp()
Page({
  data: {
    newsList: [],
    page: 1,
    is_last: 0
  },
  onLoad: function (options) {
    this.getData()
  },
  getData: function() {
    wx.showLoading({
      title: '加载中',
    })
    var that = this
    wx.request({
      url: app.globalData.apiUrl + 'news/lists',
      data: {
        page: this.data.page,
        openid: wx.getStorageSync('openid')
      },
      method: 'GET',
      success: function(res) {
        wx.hideLoading()
        if(res.statusCode == 200) {
          wx.stopPullDownRefresh()
          var result = res.data
          if(result.status == 200){
            that.setData({
              newsList: that.data.newsList.concat(result.data.item),
              is_last: result.data.is_last
            })
          } else {
            wx.showToast({
              title: result.message,
              icon: 'none'
            })
          }
        } else {
          wx.showToast({
            title: '网络请求错误，稍候重试',
            icon: 'none'
          })
        }
      }
    })
  },
  onReachBottom: function() {
    if(this.data.is_last == 1) {
      wx.showToast({
        title: '已经没有最新信息了',
        icon: 'none'
      })
    } else {
      this.setData({
        page: this.data.page + 1
      })
      this.getData()
    }
  },
  onPullDownRefresh: function() {
    this.setData({
      page: 1,
      newsList:[]
    })
    this.getData()
  }
})